package com.cssw.fyzb.busi.api.acct;

import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.busi.service.acct.RedPackService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.eos.LiveRedPackSubEo;
import com.cssw.fyzb.core.model.eos.RedPackLogEo;
import com.cssw.fyzb.core.model.vo.acct.RedPackLogVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Jian Qin.
 */
@Function(description = "查询私人发的红包", parameters = {
        @Parameter(name = "page", type = Type.String, description = "当前页", required = true),
        @Parameter(name = "limit", type = Type.String, description = "页大小", required = true),
        @Parameter(name = "userId", type = Type.Integer, description = "用户ID", required = true),
        @Parameter(name = "fromUserId", type = Type.Integer, description = "用户ID", required = true),


})
@Component
public class ListPersonSendPack implements Action {
    @Autowired
    private RedPackService redPackService;

    @Autowired
    private UserService userService;

    @Override
    public Response execute(Request request) throws Exception {
        Integer page = request.getInteger("page");
        Integer limit = request.getInteger("limit");
        Long userId = request.getLong("userId");
        Long fromUserId = request.getLong("fromUserId");

        Page<RedPackLogEo> logPage = redPackService.listPersonSendPack(fromUserId, page, limit);

        User user = userService.getById(fromUserId);

        List<RedPackLogVo> voList = ((Converter<List<RedPackLogEo>, List<RedPackLogVo>>) tmpList -> {
            List<RedPackLogVo> vos = new ArrayList<>();
            tmpList.forEach((v) -> {
                RedPackLogVo vo = new RedPackLogVo();
//                vo.setLogId(v.getLogId());
                vo.setRedPackId(v.getRedPackId());
                vo.setUserId(v.getUserId());
                vo.setMoney(v.getMoney());
                vo.setTradeId(v.getTradeId());
                vo.setTradeName(v.getTradeName());
                vo.setPackType(v.getType());
                vo.setChangeType(v.getTradeType());
                vo.setTradeId(v.getTradeId());
                vo.setCreateTime(v.getCreateTime());

                vo.setNickName(user.getNickName());
                vo.setAvatar(user.getAvatar());

                RedPackLogEo eo = redPackService.queryPersonRecieve(v.getRedPackId(), userId);
                if(eo != null){
                    vo.setIsTake("1");
                }else{
                    vo.setIsTake("0");
                }
                vos.add(vo);
            });
            return vos;
        }).convert(logPage.getList());

        return Response.success().put("list", voList).put("total", logPage.getTotalPage()).put("count", logPage.getTotalRow());
    }
}
